<?php

/**
 * This is the model class for table "advice_ext".
 *
 * The followings are the available columns in table 'advice_ext':
 * @property integer $id
 * @property integer $aid
 * @property integer $ext_id
 * @property integer $weight
 * @property string $type
 * @property integer $sort
 * @property integer $time_create
 * @property integer $time_update
 */
class AdviceExt extends CActiveRecord
{
    public $searchKey;
    public $time_begin;
    public $time_end;

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'advice_ext';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
        return array(
            array('aid, ext_id', 'required'),
            array('aid, ext_id, weight, sort, time_create, time_update', 'numerical', 'integerOnly' => true),
            array('type', 'length', 'max' => 11),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
            array('id, aid, ext_id, weight, type, sort, time_create, time_update', 'safe', 'on' => 'search'),
            array('time_begin,time_end,searchKey', 'safe'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
        return array();
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'aid' => 'Aid',
            'ext_id' => 'Ext',
            'weight' => 'Weight',
            'type' => 'Type',
            'sort' => 'Sort',
            'time_create' => 'Time Create',
            'time_update' => 'Time Update',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search()
    {
// @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('aid', $this->aid);
        $criteria->compare('ext_id', $this->ext_id);
        $criteria->compare('weight', $this->weight);
        $criteria->compare('type', $this->type, true);
        $criteria->compare('sort', $this->sort);
        $criteria->compare('time_create', $this->time_create);
        $criteria->compare('time_update', $this->time_update);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public function searchByKey()
    {
// @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('aid', $this->aid);
        $criteria->compare('ext_id', $this->ext_id);
        $criteria->compare('weight', $this->weight);
        $criteria->compare('type', $this->type, true);
        $criteria->compare('sort', $this->sort);
        $criteria->compare('time_create', $this->time_create);
        $criteria->compare('time_update', $this->time_update);

        if (!isset($_GET['Topic_sort'])) {
            $criteria->order = 't.id DESC';
        }

        if (!empty($ts_begin = strtotime($this->time_begin))) {
            $criteria->addCondition('`time_create` >= ' . $ts_begin);
        }
        if (!empty($ts_end = strtotime($this->time_end))) {
            $criteria->addCondition('`time_create` <= ' . $ts_end);
        }

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }


    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return AdviceExt the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    public static function build($aid, $extId, $type)
    {
        $model = new self();
        $model->aid = $aid;
        $model->ext_id = $extId;
        $model->type = $type;
        return $model->validate() ? $model : null;
    }
}
